/*
 * mod_coordinate_transform.c
 *
 *  Created on: 2025 Feb 18
 *      Author: SFLY
 */
#include "mod_math.h"
#include "mod_coordinate_transform.h"

/**
 ******************************************************************************
 ** \brief: CLARKE transform: a-b-c => alpha-beta
 **         three-phase stationary variables to two-phase stationary variables
 **
 ** \param [in] stc_abc_t stcabc: three-phase variables
 ** \param [in] stc_ab_t* pstcab: pointer to two-phase variables
 **
 ** \retval     none
 ******************************************************************************/
void Transf_Clarke(stc_abc_t stcabc, stc_ab_t *pstcab) {
    pstcab->Ialpha  = stcabc.Ia;
    pstcab->Ibeta   = INV_SQRT3 * stcabc.Ia + TwoINV_SQRT3 * stcabc.Ib;
}

/**
 *******************************************************************************
 ** \brief: INVERSE CLARKE transform: alpha-beta => a-b-c
 **         two-phase stationary variables to three-phase stationary variables
 **
 ** \param [in] stc_ab_t stcab: two-phase variables
 ** \param [in] stc_uvw_t* pstcabc: pointer to three-phase variables
 **
 ** \retval     none
 ******************************************************************************/
void Transf_InvClarke(stc_ab_t stcab, stc_abc_t *pstcabc) {
    pstcabc->Ia = stcab.Ialpha;
    pstcabc->Ib = SQRT3 * stcab.Ibeta - stcab.Ialpha;
    pstcabc->Ic = -(pstcabc->Ia + pstcabc->Ib);
}

/**
 *******************************************************************************
 ** \brief: PARK transform: alpha-beta => d-q
 **         two-phase stationary variables to two-phase rotation variables
 **
 ** \param [in] stc_ab_t stcab: two-phase stationary variables
 ** \param [in] stc_dq_t* pstcdq: pointer to two-phase rotation variables
 **
 ** \retval     none
 ******************************************************************************/
void Transf_Park(stc_ab_t stcab, stc_dq_t *pstcdq) {
    pstcdq->Id = stcab.Ialpha * pstcdq->cos + stcab.Ibeta * pstcdq->sin;
    pstcdq->Iq = -stcab.Ialpha * pstcdq->sin + stcab.Ibeta * pstcdq->cos;
}

/**
 *******************************************************************************
 ** \brief: INVERSE PARK transform: d-q => alpha-beta
 **         two-phase rotation variables to two-phase stationary variables
 **
 ** \param [in] stc_dq_t stcdq: two-phase rotation variables
 ** \param [in] stc_ab_t* pstcab: pointer to two-phase stationary variables
 **
 ** \retval     none
 ******************************************************************************/
void Transf_InvPark(stc_dq_t stcdq, stc_ab_t *pstcab) {
    pstcab->Ialpha  = stcdq.Id * stcdq.cos - stcdq.Iq * stcdq.sin;
    pstcab->Ibeta   = stcdq.Id * stcdq.sin + stcdq.Iq * stcdq.cos;
}
